
package client;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.jdesktop.swingx.JXEditorPane;

	public class XEditorPaneDemo extends JPanel { 
    /**
		 * 
		 */
    private static final long serialVersionUID = 1L;
		private JToolBar toolbar; 
    private JXEditorPane editor; 
     
    private ApplicationActionMap map; 
     
    /** 
     * main method allows us to run as a standalone demo. 
     */ 
    public static void main(String[] args) { 
        SwingUtilities.invokeLater(new Runnable() { 
            @Override
            public void run() { 
                //StyledTextActions.install(Application.getInstance().getContext()); 
                 
                JFrame frame = new JFrame("XEditorPaneDemo"); 
                 
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                frame.getContentPane().add(new XEditorPaneDemo()); 
                frame.setPreferredSize(new Dimension(800, 600)); 
                frame.pack(); 
                frame.setLocationRelativeTo(null); 
                frame.setVisible(true); 
            } 
        }); 
    } 
     
    public XEditorPaneDemo() { 
        super(new BorderLayout()); 
         
        createXEditorPaneDemo(); 
         
        Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this); 
         
        bind(); 
    } 
 
    private void createXEditorPaneDemo() { 
        //PENDING hack around AppFramework bug #92 
        toolbar = new JToolBar() { 
            /**
					 * 
					 */
          private static final long serialVersionUID = 1L;

						@Override 
            protected void addImpl(Component comp, Object constraints, int index) { 
                comp.setFocusable(false); 
                super.addImpl(comp, constraints, index); 
            } 
        }; 
        add(toolbar, BorderLayout.NORTH); 
         
        //URL url = XEditorPaneDemo.class.getResource("resources/Bike.html"); 
        editor = new JXEditorPane("text/html", "Thiago Henrique Busarello"); 
//        editor.setEditable(false); 
        editor.setPreferredSize(new Dimension(600, 400)); 
 
        toolbar.add(editor.getParagraphSelector()); 
 
        add(new JScrollPane(editor)); 
    } 
     
    private void bind() { 
        map = Application.getInstance().getContext().getActionMap(this); 
 
        toolbar.add(map.get("cut")); 
        toolbar.add(map.get("copy")); 
        toolbar.add(map.get("paste")); 
        toolbar.addSeparator(); 
        //TODO doesn't work this way because the action is uninstalled and reinstalled every call 
        toolbar.add(editor.getActionMap().get("undo")); 
        toolbar.add(editor.getActionMap().get("redo")); 
        toolbar.addSeparator(); 
        toolbar.add(new JToggleButton(map.get("bold"))); 
        toolbar.add(new JToggleButton(map.get("italic"))); 
        toolbar.add(new JToggleButton(map.get("underline"))); 
        toolbar.add(editor.getParagraphSelector()); 
         
        //TODO this is the "SwingX" way to do proxy style actions. 
        //AbstractActionExt[] actions = new AbstractActionExt[14]; 
//        List<AbstractActionExt> actions = new ArrayList<AbstractActionExt>(); 
//        actions.add(ActionFactory.createTargetableAction("insert-break", "LF", "F")); 
// 
//        actions.add(ActionFactory.createTargetableAction("left-justify", "Left", "L", true, 
//                                                          "position-group")); 
//        actions.add(ActionFactory.createTargetableAction("center-justify", "Center", "C", true, 
//                                                          "position-group")); 
//        actions.add(ActionFactory.createTargetableAction("right-justify", "Right", "R", true, 
//                                                          "position-group")); 
// 
//        actions.add(ActionFactory.createTargetableAction("font-bold", "Bold", "B", true)); 
//        actions.add(ActionFactory.createTargetableAction("font-italic", "Italic", "I", true)); 
//        actions.add(ActionFactory.createTargetableAction("font-underline", "Underline", "U", true)); 
// 
//        actions.add(ActionFactory.createTargetableAction("InsertUnorderedList", "UL", "U", true)); 
//        actions.add(ActionFactory.createTargetableAction("InsertOrderedList", "OL", "O", true)); 
//        actions.add(ActionFactory.createTargetableAction("InsertHR", "HR", "H")); 
//        ActionManager manager = ActionManager.getInstance(); 
//         
//        List<Object> actionNames = new ArrayList<Object>(); 
//        for (AbstractActionExt ext : actions) { 
//            manager.addAction(ext); 
//            actionNames.add(ext.getActionCommand()); 
//             
//        } 
//         
//        // Populate the toolbar. Must use the ActionContainerFactory to ensure 
//        // that toggle actions are supported. 
//        ActionContainerFactory factory = new ActionContainerFactory(manager); 
// 
//        toolbar = factory.createToolBar(actionNames); 
//        add(toolbar, BorderLayout.NORTH); 
    } 
} 